<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" label-width="68px" class="form-flex">
      <el-row :gutter="20">
        <el-col :span="6" :xs="24">
          <el-form-item label="合同类型" prop="contractCategoryId">
            <contract-category-filterable
              v-model="queryParams.contractCategoryId"
              ref="contractRefs"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6" :xs="24">
          <el-form-item label="合同编号" prop="no">
            <el-input
              v-model="queryParams.no"
              placeholder="请输入合同编号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6" :xs="24">
          <el-form-item label="状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
              <el-option
                v-for="item in dict.lease_contract_status"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6" :xs="24">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary"
                   plain icon="el-icon-plus"
                   size="mini"
                   @click="handleAdd"
                   v-hasPermi="['lease:leaseContract:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success"
                   plain icon="el-icon-edit"
                   size="mini"
                   :disabled="single"
                   @click="handleUpdate"
                   v-hasPermi="['lease:leaseContract:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger"
                   plain icon="el-icon-delete"
                   size="mini"
                   :disabled="multiple"
                   @click="handleDelete"
                   v-hasPermi="['lease:leaseContract:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning"
                   plain icon="el-icon-download"
                   size="mini"
                   :loading="exportLoading"
                   @click="handleExport"
                   v-hasPermi="['lease:leaseContract:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="leaseContractList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="合同类型" align="center" prop="typeName"/>
      <el-table-column label="合同编号" align="center" prop="no"/>
      <el-table-column label="出租房源" align="center" prop="roomName"/>
      <el-table-column label="租户名称" align="center" prop="cusName"/>
      <el-table-column label="合同开始日期" align="center" prop="beginTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.beginTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同结束日期" align="center" prop="finishTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.finishTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="租赁期限" align="center" prop="allottedTime">
        <template slot-scope="scope">
          <span v-if="scope.row.allottedTime">{{scope.row.allottedTime}}个月</span>
        </template>
      </el-table-column>
      <el-table-column label="签署日期" align="center" prop="signTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.signTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出租面积m²" align="center" prop="leaseArea"/>
<!--      <el-table-column label="经手人" align="center" prop="createBy"/>-->
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          {{ dict.label.lease_contract_status[scope.row.status] }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-video-play"
            @click="handleApple(scope.row)"
            v-hasPermi="['lease:leaseContract:appleLease']"
            v-if="scope.row.status=='10'"
          >履行合同
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-plus"
            @click="handleBefore(scope.row)"
            v-hasPermi="['lease:leaseContract:beforeRecord']"
            v-if="scope.row.status=='20'"
          >变更合同
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleRecord(scope.row)"
            v-hasPermi="['lease:LeaseRecord:list']"
            v-if="scope.row.reletStatus=='1'"
          >变更记录
          </el-button>
          <el-button
          size="mini"
          type="text"
          icon="el-icon-circle-close"
          v-hasPermi="['lease:leaseContract:expriyLease']"
          @click="handleExpiry(scope.row)"
          v-if="scope.row.status=='20'"
        >终止
        </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-chat-line-round"
            @click="handleRelet(scope.row)"
            v-hasPermi="['lease:leaseContract:add']"
            v-if="scope.row.status=='30'"
          >续租
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <LeaseContractForm ref="formRef" @cancel="getList"/>
    <expiry-form ref="expiryRef" @cancel="getList"/>
    <relet-form ref="reletRef" @cancel="getList"/>
    <before-form ref="beforeRef"  @cancel="getList"/>
    <record-form ref="recordRed"  @cancel="getList"/>
  </div>
</template>

<script>

  import {listMixin} from "@/utils/mixins/listMixin";
  import {listLeaseContract, delLeaseContract, exportLeaseContract,appleLease,getLeaseContract} from "@/api/lease/leaseContract";
  import LeaseContractForm from "./form.vue"
  import expiryForm from "./expiryForm";
  import reletForm from "./reletForm";
  import beforeForm from "./beforeForm";
  import recordForm from "./recordForm";
  export default {
    name: "LeaseContract",
    components: {LeaseContractForm,expiryForm,reletForm,beforeForm,recordForm},
    mixins: [listMixin],
    dicts: ['business_status', 'brand_level ', 'lease_contract_status',],
    data() {
      return {
        // 租赁合同表格数据
        leaseContractList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          contractCategoryId: null,
          no: null,
          status: null,
        },
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询租赁合同列表 */
      getList() {
        this.loading = true;
        listLeaseContract(this.queryParams).then(response => {
          this.leaseContractList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.nos=selection.map(item => item.no)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.$refs.formRef.show({"type": "ADD"});
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        const id = row.id || this.ids
        getLeaseContract(id[0]).then(res=>{
          if(res.data.status=='10'){
            const option = {
              "type": "UPDATE",
              "id": id
            }
            this.$refs.formRef.show(option);
          }else {
            this.msgError("预定合同状态下才可修改");
          }
        })

      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        const nos = row.no ||this.nos;
        this.$confirm('是否确认删除租赁合同编号为"' + nos + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delLeaseContract(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      handleApple(row){
        this.$confirm('是否确定履行该合同?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          return appleLease(row);
        }).then(() => {
            this.getList();
            this.msgSuccess("履行该合同成功");
        })
      },
      //退租
      handleExpiry(row){
        const id = row.id
        const option = {
          "id": id
        }
        this.$refs.expiryRef.show(option);
      },
      //续租
      handleRelet(row){
        const id = row.id
        const option = {
          "id": id
        }
        this.$refs.reletRef.show(option);
      },
      //变更
      handleBefore(row){
        const id = row.id
        const option = {
          "id": id
        }
        this.$refs.beforeRef.show(option);
      },
      //变更记录
      handleRecord(row){
        const id = row.id
        const option = {
          "id": id
        }
        this.$refs.recordRed.show(option);
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有租赁合同数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportLeaseContract(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {
        });
      }
    }
  };
</script>
